package com.example.pt.repository;

import com.example.pt.entity.*;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
//import net.sf.jsqlparser.statement.select.Values; // 确认是否能正确导入
//import com.github.jsqlparser.statement.select.Values; // 新版本包名
@Repository
public interface TorrentPromotionRepository extends JpaRepository<TorrentPromotion, Integer> {
    List<TorrentPromotion> findByTorrentIdAndEndTimeAfter(Integer torrentId, LocalDateTime currentTime);

    boolean existsByTorrentIdAndPromotionTypeAndStartTimeBetween(
            Integer torrentId, Integer promotionType,
            LocalDateTime startTime, LocalDateTime endTime);

//    @Modifying
//    @Transactional
//    @Query("DELETE FROM TorrentPromotion tp WHERE tp.endTime < :currentTime")
//    int deleteByEndTimeBefore(@Param("currentTime") LocalDateTime currentTime);
//    @Query(value =
//            "SELECT tp.* " +
//                    "FROM torrent_promotion tp " +
//                    "JOIN torrent_info_hash tih ON tp.tid = tih.tid " +
//                    "WHERE tih.info_hash = :infoHash " +  // 使用数据库字段名 info_hash
//                    "AND tp.start_time <= NOW() " +       // 使用 MySQL 函数 NOW()
//                    "AND tp.end_time > NOW()",
//            nativeQuery = true)                  // 启用原生 SQL
//    List<TorrentPromotion> getCurrentPromotionsByInfoHash(@Param("infoHash") String infoHash);
}